import { CONF_CONFIG } from './config.js';

let Economy  = {};
let EconomyType = CONF_CONFIG.get('经济系统')["类型"]; // llmoney、scoreboard、func
let EconomyScoreBoardName = CONF_CONFIG.get('经济系统')["计分板名称"];
//let EconomyFunc = CONF_CONFIG.get('经济系统')["经济函数"];

// 经济API
let EconomyFunc = CONF_CONFIG.get('经济系统')["经济函数"];
if (CONF_CONFIG.get('经济系统')["类型"] == 'func'){
    if (ll.hasExported(EconomyFunc.namespace, EconomyFunc.getMoney)){
        Economy._getMoney = ll.imports(EconomyFunc.namespace, EconomyFunc.getMoney);
        Economy._setMoney = ll.imports(EconomyFunc.namespace, EconomyFunc.setMoney);
        Economy._addMoney = ll.imports(EconomyFunc.namespace, EconomyFunc.addMoney);
        Economy._reduceMoney = ll.imports(EconomyFunc.namespace, EconomyFunc.reduceMoney);
    }else{
        logger.error(`未检测到已导出的自定义经济函数(${EconomyFunc.namespace}), 请检查前置自定义经济插件`);
    }
}


export function test(pl){
    let fm_eco = mc.newSimpleForm()
    .setTitle(`经济测试`)
    .addButton(`§lGet`)
    .addButton(`§lSet 10`)
    .addButton(`§lAdd 10`)
    .addButton(`§lReduce 7`);
    
    pl.sendForm(fm_eco, (pl, id)=>{
        if (id == null){return;}
        if (id == 0){
            let res = getMoney(pl.xuid);
            pl.sendToast(`Get`, `[${res}] Money:§e ${getMoney(pl.xuid)}`);
            pl.tell(`[${res}] Get§e ${getMoney(pl.xuid)}`);
            return ECOtest(pl);
        }
        if (id == 1){
            let res = setMoney(pl.xuid, 10);
            pl.sendToast(`Set`, `[${res}] Set 10 | Money:§e ${getMoney(pl.xuid)}`);
            pl.tell(`[${res}] Set 10 | Money:§e ${getMoney(pl.xuid)}`);
            return ECOtest(pl);
        }
        if (id == 2){
            let res = addMoney(pl.xuid, 10);
            pl.sendToast(`Add`, `[${res}] Add 10 | Money:§e ${getMoney(pl.xuid)}`);
            pl.tell(`[${res}] Add 10 | Money:§e ${getMoney(pl.xuid)}`);
            return ECOtest(pl);
        }
        if (id == 3){
            let res = reduceMoney(pl.xuid, 7);
            pl.sendToast(`Reduce`, `[${res}] Reduce 7 | Money:§e ${getMoney(pl.xuid)}`);
            pl.tell(`[${res}] Reduce 7 | Money:§e ${getMoney(pl.xuid)}`);
            return ECOtest(pl);
        }
    });
}

export function getMoney(xuid){
    if (EconomyType == 'llmoney'){
        return money.get(xuid);   
    }
    if (EconomyType == 'scoreboard'){
        let uuid = data.xuid2uuid(xuid);
        if (uuid == null){return null;}
        let res = mc.getPlayerScore(uuid, EconomyScoreBoardName);
        if (res == undefined){res = null;}
        return res;
    }
    if (EconomyType == 'func'){
        return Economy._getMoney(xuid);
    }
}

export function addMoney(xuid, value, note = ''){
    if (EconomyType == 'llmoney'){
        return money.add(xuid, value);   
    }
    if (EconomyType == 'scoreboard'){
        let uuid = data.xuid2uuid(xuid);
        if (uuid == null){return false;}
        return mc.addPlayerScore(uuid, EconomyScoreBoardName, value);
    }
    if (EconomyType == 'func'){
        return Economy._addMoney(xuid, value, note);
    }
}

export function reduceMoney(xuid, value, note = ''){
    if (EconomyType == 'llmoney'){
        return money.reduce(xuid, value);   
    }
    if (EconomyType == 'scoreboard'){
        let uuid = data.xuid2uuid(xuid);
        if (uuid == null){return false;}
        let balance = getMoney(xuid);
        if (balance == null){return false;}
        if (getMoney(xuid) < value){return false;}
        return mc.reducePlayerScore(uuid, EconomyScoreBoardName, value);
    }
    if (EconomyType == 'func'){
        let balance = Economy._getMoney(xuid);
        if (balance == null){return false;}
        if (balance < value){return false;}
        return Economy._reduceMoney(xuid, value, note);
    }
}

export function setMoney(xuid, value, note = ''){
    if (EconomyType == 'llmoney'){
        return money.set(xuid, value);   
    }
    if (EconomyType == 'scoreboard'){
        let uuid = data.xuid2uuid(xuid);
        if (uuid == null){return false;}
        return mc.setPlayerScore(uuid, EconomyScoreBoardName, value);
    }
    if (EconomyType == 'func'){
        return Economy._setMoney(xuid, value, note);
    }
}